package com.zb.test;

import java.sql.*;

public class JdbcTest2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://127.0.0.1:3306/java-web?useUnicode=true&characterEncoding=UTF8&useSSL=true";
        String username = "root";
        String password = "root";

        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建链接
        Connection connection = DriverManager.getConnection(url, username, password);
        //编写sql
        String sql = "insert into users (id, name, password, email, birthday) values (?, ?, ?, ?, ?);";
        //创建预编译对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 4);
        preparedStatement.setString(2, "张彬");
        preparedStatement.setString(3, "root");
        preparedStatement.setString(4, "594507903@qq.com");
        preparedStatement.setDate(5, new Date(new java.util.Date().getTime()));
        //执行sql
        int i = preparedStatement.executeUpdate();

        if(i > 0){
            System.out.println("插入成功！");
        }

        preparedStatement.close();
        connection.close();
    }
}
